package com.iman.manjusri.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iman.common.support.Page;
import com.iman.common.util.ConfigUtil;
import com.iman.manjusri.service.ArticlesService;
import com.iman.manjusri.vo.ArticlesVo;
import com.iman.manjusri.vo.CaseVo;
import com.iman.manjusri.vo.CourseAssistInfoVo;
import com.iman.manjusri.vo.CourseDetailVo;
import com.iman.manjusri.vo.CourseKeShiVo;
import com.iman.manjusri.vo.CourseRecommendVo;
import com.iman.manjusri.vo.CourseTeacherVo;
import com.iman.manjusri.vo.ShareExpertVo;

/**
 * 
 * @author Thinkpad
 *
 *德瑞姆网校资讯      通知公告      学院动态   行业动态   考试指南   备考指南    ，的页面
 *
 */ 

@Controller
@RequestMapping(value = "/case/*")
public class CaseController extends BaseController{

	@Autowired
	private ArticlesService articlesServiceImpl;

    //图片的域名  	   
	 String imgbasePath  =ConfigUtil.getString( "image.server" );; 
	 //css 服务器连接    在 context.properties 配置
	 String cssbasePath  =ConfigUtil.getString( "css.server" );; 
	//js 	url base	服务器连接    在 context.properties 配置
	 String jsbasePath  =ConfigUtil.getString( "js.server" );; 
	 //为了jsp页面的 正确显示 
	 String cssfeedbackPath  =ConfigUtil.getString( "feedback.domain" );; 
 
	
	//查询分页显示案例分享的分页数据
	@RequestMapping(value = "/{type}/{pcnt}/{page}")
	@ResponseBody()
	public Page  listCaseByPage (
		 @Valid    @PathVariable  	String	type
		,@Valid    @PathVariable  	String	page 
		,@Valid    @PathVariable  	String	pcnt 
	  ){
		
		//分页的页数  如果没有的话 ，为0  第一页
		int pageInt = NumberUtils.toInt(page,0);
		//每页多少个
		int perPageInt = NumberUtils.toInt(pcnt,10);
		//类型
		Integer typeInt = NumberUtils.toInt(type,10);
		
		Map params = new HashMap();
		params.put("pageSize", perPageInt);
		params.put("pageIndex", pageInt);
		params.put("type", typeInt);
		
		//查询数据
		Page<CaseVo> pages = articlesServiceImpl.listCaseByPage(params);
		
		
		
		
		return pages;
	}
	
	 /**
	  *  显示最新案例和热门案例
	  * @param type
	  * @return
	  */
	  @RequestMapping(value = "/listTopCase")
	  public ModelAndView listTopNewCase(@NotNull @RequestParam(required = true)  String type){
		  Map<String,Object> result = new  HashMap<String,Object>();
		  ModelAndView view = null;
		  
		   //类型	
			Integer typeInt = NumberUtils.toInt(type,10);
			//当前的页数  如果没有的话 ，为0  第一页
			int pageInt =5;
			//每页多少条记录
			int perPageInt = 0;
			Map params = new HashMap();
			params.put("type", typeInt);
			params.put("pageIndex", pageInt);
			params.put("pageSize", perPageInt);
			
			//查询数据
			Page<CaseVo> pages = articlesServiceImpl.listTopNewCase(params);
			Page<CaseVo> pagesHot = articlesServiceImpl.listTopHotCase(params);
		  
			//System.out.println("*************tilte="+pages.getData().get(0).getTilte()+"caseid="+pages.getData().get(0).getCase_id());
			//System.out.println("*************tilte"+pages.getData().get(1).getTilte());
			
		  result.put("caseTopNew", pages.getData());
		  result.put("caseTopHot", pagesHot.getData());
		  
		  view = new ModelAndView("study_case",result);
		  Page<ShareExpertVo> shareExpertPage = articlesServiceImpl.shareExpertList(1, 5);
		  view.addObject("shareExpertList",shareExpertPage.getData());
		  view.addObject("shareExpertNext", String.valueOf(shareExpertPage.hasNextPage()));     
		  putResourceSrc(view);	
		  
		  return view;
	  }
	  
	  @RequestMapping(value = "/jsonTopCase")
	  @ResponseBody()
	  public Map jsonTopNewCase(@NotNull @RequestParam(required = true)  String type){
		  Map<String,Object> result = new  HashMap<String,Object>();
		  
		   //类型	
			Integer typeInt = NumberUtils.toInt(type,10);
			//分页的页数  如果没有的话 ，为0  第一页
			int pageInt =5;
			//每页多少个
			int perPageInt = 0;
			Map params = new HashMap();
			params.put("type", typeInt);
			params.put("pageIndex", pageInt);
			params.put("pageSize", perPageInt);
			
			//查询数据
			Page<CaseVo> pages = articlesServiceImpl.listTopNewCase(params);
			Page<CaseVo> pagesHot = articlesServiceImpl.listTopHotCase(params);
		  
			//System.out.println("*************tilte="+pages.getData().get(0).getTilte()+"userid="+pages.getData().get(0).getUser_id());
			//System.out.println("*************tilte"+pages.getData().get(1).getTilte());
			
		  result.put("caseTopNew", pages.getData());
		  result.put("caseTopHot", pagesHot.getData());
		  
		     
		  return result;
	  }
	  
	  
	   //查询更多最新案例的分页数据
		@RequestMapping(value = "/{type}/{pcnt}/{page}/{moreTabCaseTp}")
		@ResponseBody()
		public Page  listMoreCaseByPage (
			 @Valid    @PathVariable  	String	type
			,@Valid    @PathVariable  	String	page 
			,@Valid    @PathVariable  	String	pcnt
			,@Valid    @PathVariable  	String	moreTabCaseTp
		  ){
			
			//分页的页数  如果没有的话 ，为0  第一页
			int pageInt = NumberUtils.toInt(page,0);
			//每页多少个
			int perPageInt = NumberUtils.toInt(pcnt,10);
			//类型
			Integer typeInt = NumberUtils.toInt(type,10);
			
			Integer tabCaseTp = NumberUtils.toInt(moreTabCaseTp,10);
			
			Map params = new HashMap();
			params.put("pageSize", perPageInt);
			params.put("pageIndex", pageInt);
			params.put("type", typeInt);
			
			Page<CaseVo> pages;
			if(tabCaseTp==0){
			//查询数据
			  pages = articlesServiceImpl.listTopNewCase(params);
			}
			else
			{
			  pages = articlesServiceImpl.listTopHotCase(params);	
			}
			 
			
			return pages;
		} 
		 
		/**
		 * 显示案例详情 
		 * @param caseId
		 * @return ModelAndView
		 */
		@RequestMapping(value = "/getCaseDetail")
		public ModelAndView getCaseDetail(@NotNull @RequestParam(required = true)  String caseId){
			
			Map<String,Object> result = new  HashMap<String,Object>();
			ModelAndView view = null;
			//传递案例id参数
			Map params = new HashMap();
			params.put("caseId", caseId);
			//分页查询数据
			Page<CaseVo> pages = articlesServiceImpl.getCaseById(params);
			if(pages.getData().size()==0)
			{
				view = new ModelAndView("error",null);
				return view;
			}
			result.put("caseDetail", pages.getData().get(0));
			view = new ModelAndView("study_case_detail",result);
			Page<ShareExpertVo> shareExpertPage = articlesServiceImpl.shareExpertList(1, 5);
			view.addObject("shareExpertList",shareExpertPage.getData());
			view.addObject("shareExpertNext", String.valueOf(shareExpertPage.hasNextPage()));
			//公共资源 
			putResourceSrc(view);	
			
			
			return view;
		}
		
		/**
		 * 获取评论人的头像
		 * @param user_id
		 * @param comment_id
		 * @return
		 */
		@RequestMapping("/resource/getHeadImg/{user_id}/{comment_id}")
		public @ResponseBody Map getHeadImgAjax(@PathVariable long user_id,@PathVariable String comment_id,HttpServletRequest request){
			Map<String,Object> result = new HashMap<String,Object>();		
			if(!StringUtils.equals(comment_id, "null"))
				result.put("comment_id", comment_id);
			if(user_id == 0)
				user_id = this.getUserIdInSession(request);
			System.out.println("userid***********"+user_id);
			ShareExpertVo user = this.articlesServiceImpl.commentUserHead(user_id);
			result.put("himg", user!=null?user.getUser_himg():"");
			result.put("uid", user_id);
			result.put("uname", user.getUser_lnm());
			return result;
		}


		/**
		 * 浏览量+1
		 * @param resource_id
		 * @return
		 */
		@RequestMapping(value="/resource/resourceViewAjax/{resource_id}")
		public @ResponseBody int resourceViewAjax(@PathVariable long resource_id){
			this.articlesServiceImpl.incrViewCnt(resource_id);
			return 0;
		}
		
		/**
		 * 获取分享达人列表
		 * @param request
		 * @return
		 */
		@RequestMapping(value="/resource/expertListAjax/{pageNum}")
		public @ResponseBody Map shareExpertListAjax(HttpServletRequest request,@PathVariable int pageNum){
			Map<String,Object> map = new HashMap<String,Object>();
			Page<ShareExpertVo> shareExpertPage = articlesServiceImpl.shareExpertList(pageNum, 5);
			map.put("shareExpertList",shareExpertPage.getData());
			map.put("shareExpertNext", String.valueOf(shareExpertPage.hasNextPage()));
			return map;
		}
		/********************************加入资源路径*********************************************************/
		
	
	
	
		/**
		 * 
		 *  Function:
		 *  功能说明： 获取session  获取sessionid  用户ID  
		 *	 使用说明：
		 *  @author  wangxiaolei  DateTime 2014年12月18日 下午12:24:48
		 *	返回类型: SessionBean    
		 *  @param request
		 *  @return
		 */
		public Long getUserIdInSession(HttpServletRequest request){
			 return super.getUserIdInSession(request);
		}
		
}
